import BaseModel from "../../cfw/mvc/BaseModel";
import ModelManager from "../../cfw/mvc/ModelManager";
import SnowToyModel from "./model/SnowToyModel";
import SnowBodyModel from "./model/SnowBodyModel";
import SnowTypeModel from "./model/SnowTypeModel";
import ModuleManager from "../../cfw/module/ModuleManager";
import User from "../db/User";


export default class SnowManManager extends BaseModel {
    private static ins: SnowManManager;

    static instance() {
        if (!this.ins) {
            this.ins = new SnowManManager()
        }
        return this.ins;
    }


    protected toys: ModelManager<SnowToyModel> = new ModelManager()

    protected bodys: ModelManager<SnowBodyModel> = new ModelManager()

    protected bodyType: ModelManager<SnowTypeModel> = new ModelManager()


    constructor() {
        super();
        this.toys.initWithData(ModuleManager.dataManager.get(SnowToyModel.CLASS_NAME), SnowToyModel)
        this.bodys.initWithData(ModuleManager.dataManager.get(SnowBodyModel.CLASS_NAME), SnowBodyModel)
        this.bodyType.initWithData(ModuleManager.dataManager.get(SnowTypeModel.CLASS_NAME), SnowTypeModel)
    }

    getTypeList() {
        return this.bodyType.getList()
    }

    getToyByID(id: number) {
        return this.toys.getByID(id)
    }


    getType(typeID: number) {
        return this.bodyType.getByID(typeID)
    }

    getBodyList() {
        return this.bodys.getList();
    }

    getBodyByIndex(index: number) {
        return this.bodys.getByIndex(index)
    }

    getToyList() {
        return this.toys.getList()
    }

    getUseToy(index: number) {

        let id = User.instance().getSnowToyData(index)
        if (id > 0) {
            return this.toys.getByID(id)
        }
        return null;
    }

    getUseBoy(index: number) {
        let id = User.instance().getSnowBodyTypeData(index)
        if (id > 0) {
            return this.bodys.getByID(id)
        } else {
            return null;
        }
    }

    setUseToy(index: number, model: SnowToyModel) {
        User.instance().setSnowToyData(index, model.getID())
    }
}
